﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CDB.BusinessLayer.Colunas;
using CDB.BusinessLayer.Tabelas;
using CDB.BusinessLayer.Utilitarios;
using CDB.Model.Colunas;
using CDB.Model.Tabelas;


namespace ColaDaBase
{
    /// <summary>
    /// Interaction logic for w_GerarProc.xaml
    /// </summary>
    public partial class w_GerarProc : Window
    {
        private string strConnection = @"Data Source=RENAN-B114941BE\SQLEXPRESS;Initial Catalog=dbFrameWork;Integrated Security=True;Pooling=False";
        private string strProvoder = "System.Data.SQLClient";
        private List<string> gstrTabelasSelecionadas = new List<string>();

        #region Construtor
        public w_GerarProc()
        {
            InitializeComponent();
        }
        #endregion

        #region Eventos
        protected override void  OnInitialized(EventArgs e)
        {
 	         base.OnInitialized(e);
             this.Bind();
        }

        #region DataGrid
        
        private void dtgTabelas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            dtgTabelas.UnselectAll();

        }
        #endregion

        #region Botões
        private void btnGerarProcs_Click(object sender, RoutedEventArgs e)
        {

            this.GerarProcs();

        }
        #endregion

        #endregion

        #region Métodos
        protected void Bind()
        {
            BLTabela objBLTabela = new BLTabela(this.strConnection,this.strProvoder);

            try 
	        {	        
		        dtgTabelas.ItemsSource = objBLTabela.Listar();
	        }
	        catch (Exception ex)
	        {
		
		        MessageBox.Show(ex.Message);
	        }
        }

        private void GerarProcs()
        {
            List<string> lstNomeTableas = new List<string>();
            BLUtilitarios objBLUtilitarios = new BLUtilitarios(this.strConnection,this.strProvoder);

            for (int i = 0; i < dtgTabelas.Items.Count; i++)
            {
                DataGridRow row = (DataGridRow)dtgTabelas.ItemContainerGenerator.ContainerFromIndex(i);
                CheckBox cellContent = dtgTabelas.Columns[0].GetCellContent(row) as CheckBox;
                if (cellContent != null && cellContent.IsChecked == true)
                {
                    TextBlock txtNomeTabela = dtgTabelas.Columns[1].GetCellContent(row) as TextBlock;
                    if (txtNomeTabela != null && txtNomeTabela.Text != string.Empty)
                    {
                        lstNomeTableas.Add(txtNomeTabela.Text);
                    }
                }
            }

            this.txtProcs.Text = string.Empty;
            foreach (string strNomeTabela in lstNomeTableas)
            {
                this.txtProcs.Text += objBLUtilitarios.GerarProcedures(strNomeTabela) + "\n\n";
            }
        }
        #endregion

        private void txtProcs_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

       
    }
}
